package com.modules.tag;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

/**
 * Created by lewis on 2017/6/16.
 */
public class TableTag extends SimpleTagSupport implements OptionSupport{


    private static final long	serialVersionUID	= -1670780497875545031L;




    private String id;




    OptionSupportHandler handler = new OptionSupportHandler();

    /**
     * 增加选项，同名选项将被覆盖
     */
    public void addOption(Option opt) {
        handler.addOption(opt);
    }

    /**
     * 增加一个选项
     * @param name
     * @param value
     */
    public void addOption(String name, String value)
    {
        Option option = OptionUtils.createOption(name, value);
        addOption(option);
    }


    public List<Option> getOptions() {
        return handler.getOptions();
    }
    public boolean contains(String key) {
        return handler.contains(key);
    }
    public String getJSExpression() {
        return handler.getJSExpression();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void addColumn(Column column)
    {
        handler.addColumn(column);
    }

    public void doTag() throws JspException,IOException {

        handler.clear();

        getJspBody().invoke(null);



            Writer out = getJspContext().getOut();
            try
            {
                out.write("<table  id=\""+id+"\" lay-filter=\""+id+"_lay_filter\" ></table>");
				/*输出 datagrid 脚本*/
                out.write("<script type=\"text/javascript\">");
                out.write("layui.use(['table','jquery','layer'], function(){");
                out.write("var table = layui.table, $ = layui.jquery;");
                //设置默认参数
                out.write(" var args = { elem: '"+id+"' ,id:'user_info'");
                out.write(",url: '/user/userInfo/query'");
                out.write(",method:'post'");
                out.write(",width: '100%'");
                out.write(",limits: [10,15,20,30,50,100]");
                out.write(",limit: 15");
                out.write(",even: true}");

                //进行参数覆盖

                out.write("table.render(");
                out.write(getJSExpression());
                out.write(")");

                //方法级渲染

            } catch(IOException ex)
            {
                ex.printStackTrace();
            }
        }

    }

